package com.xuebank.nfe.nfecar.activity

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Bitmap
import android.os.*
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Toast

import com.trello.rxlifecycle.android.ActivityEvent
import com.wzn.libaray.event.RxBus
import com.wzn.libaray.utils.Logger
import com.xuebank.nfe.nfecar.*
import com.xuebank.nfe.nfecar.beas.BaseActivity
import com.xuebank.nfe.nfecar.config.PictureConfig
import com.xuebank.nfe.nfecar.entity.LocalMedia
import com.xuebank.nfe.nfecar.globals.Constants
import com.xuebank.nfe.nfecar.model.PreviewModel
import com.xuebank.nfe.nfecar.view.camera.JCameraView
import com.xuebank.nfe.nfecar.view.camera.listener.ClickListener
import com.xuebank.nfe.nfecar.view.camera.listener.ErrorListener
import com.xuebank.nfe.nfecar.view.camera.listener.JCameraListener
import com.xuebank.nfe.nfecar.view.camera.util.DeviceUtil
import com.xuebank.nfe.nfecar.view.camera.util.FileUtil
import com.yalantis.ucrop.UCrop
import com.yalantis.ucrop.UCropMulti
import java.io.File
import java.io.Serializable
import java.util.ArrayList

/**
 * 相机 、照片 、拍摄视频界面
 */

class CameraActivity : BaseActivity() {
    private var jCameraView: JCameraView? = null
    var path:String ? = null
    private var selectImages: MutableList<LocalMedia>? = ArrayList()
    var mLocalMedia :LocalMedia? = null

    companion object {
        var mCameraActivity:CameraActivity ? =null
        fun setUp(context: Context) {
            val intent = Intent(context, CameraActivity::class.java)
            context.startActivity(intent)
        }
        val END_OF_PHOTO = "END_OF_PHOTO"
    }

    override fun initViews() {
        super.initViews()
        RxBus.getDefault().toObserverable(PreviewModel::class.java)
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe { result ->
                    if (result?.type == "photo") {
                        val path= FileUtil.saveBitmap("JCamera", result.bitmap)
                        mLocalMedia = LocalMedia()
                        mLocalMedia?.path = path
                        selectImages?.add(mLocalMedia!!)

                        val intent = Intent(this, ReleaseActivity::class.java)
                        val bundle = Bundle()

                        bundle.putString("Imagepath", path)
                        intent.putExtra(Constants.LOGIN_RELEASE,Constants.ALBUM)
                        intent.putExtra("hhaha","hahaw")

                        bundle.putSerializable(PictureConfig.EXTRA_SELECT_LIST, selectImages as Serializable)
//                        bundle.putBoolean(PictureConfig.EXTRA_BOTTOM_PREVIEW, true)
                        intent.putExtras(bundle)
                        startActivity(intent)

                        RxBus.getDefault().post("mteuskl")
//                        startActivity(PicturePreviewActivity::class.java, bundle,
//                                if (config.selectionMode == PictureConfig.SINGLE) UCrop.REQUEST_CROP else UCropMulti.REQUEST_MULTI_CROP)

                    }else if (result?.type == "video") {
                        val intent = Intent(this, NativeVideoPlayerActivity::class.java)

                        intent.putExtra("path",result.url)
                        intent.putExtra(Constants.LOGIN_RELEASE,Constants.ALBUM)
                        startActivity(intent)
                        Toast.makeText(this,"选择了视频",Toast.LENGTH_SHORT).show()
                        RxBus.getDefault().post("mteuskl")
                    }
                }



        RxBus.getDefault().toObserverable(String::class.java)
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe { result ->
                    if (result == MainActivity.PHOTOGRAPH) {
                        jCameraView?.setFeatures(JCameraView.BUTTON_STATE_ONLY_CAPTURE)
                    }else if (result == MainActivity.VIDEO) {
                        jCameraView?.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER)
                    }else if (result == MainActivity.CHOOSING_ALBUMS) {

                        val intent = Intent(this, ReleaseActivity::class.java)
                        intent.putExtra(Constants.LOGIN_RELEASE,Constants.ALBUM)
                        startActivity(intent)
                    }else if (result == "clone") {
                        this.finish()
                    }

                }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mCameraActivity = this
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        setContentView(R.layout.activity_camera)

        jCameraView = findViewById(R.id.jcameraview) as JCameraView
        //设置视频保存路径
        jCameraView?.setSaveVideoPath(Environment.getExternalStorageDirectory().path + File.separator + "JCamera")

        jCameraView?.setFeatures(JCameraView.BUTTON_STATE_BOTH)
        jCameraView?.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE)
        jCameraView?.background?.alpha=0
        jCameraView?.setErrorLisenter(object : ErrorListener {
            override fun onError() {
                //错误监听
                Log.i("CJT", "camera error")
                val intent = Intent()
                setResult(103, intent)
                finish()
            }

            override  fun AudioPermissionError() {
                Toast.makeText(this@CameraActivity, "给点录音权限可以?", Toast.LENGTH_SHORT).show()
            }
        })
        //JCameraView监听
        jCameraView?.setJCameraLisenter(object : JCameraListener {
            override fun captureSuccess(bitmap: Bitmap) {
                //获取图片bitmap
                //                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
                path = FileUtil.saveBitmap("JCamera", bitmap)
                val intent = Intent()
                intent.putExtra("Imagepath", path)


            }

            override fun recordSuccess(url: String, firstFrame: Bitmap) {
                //获取视频路径
                val path = FileUtil.saveBitmap("JCamera", firstFrame)
                Log.i("CJT", "url = $url, Bitmap = $path")
                val intent = Intent()
                intent.putExtra("path", path)
                setResult(101, intent)
                finish()
            }
        })

        jCameraView?.setLeftClickListener { this@CameraActivity.finish() }
        jCameraView?.setRightClickListener { Toast.makeText(this@CameraActivity, "Right", Toast.LENGTH_SHORT).show() }

        Log.i("CJT", DeviceUtil.getDeviceModel())

    }

    override fun onStart() {
        super.onStart()
        //全屏显示
        if (Build.VERSION.SDK_INT >= 19) {
            val decorView = window.decorView
            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        } else {
            val decorView = window.decorView
            val option = View.SYSTEM_UI_FLAG_FULLSCREEN
            decorView.systemUiVisibility = option
        }
    }

    override fun onResume() {
        super.onResume()
        RxBus.getDefault().post("END_OF_PHOTO")
        jCameraView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        jCameraView?.onPause()
    }

}
